<!doctype html><html lang=cn-zh><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta http-equiv=X-UA-Compatible content="IE=edge"><style type=text/css>body{font-family:monospace}</style><title>自助购物机交易源码</title>
<meta name=description content="A blog maintained by Vimiix."><link rel=stylesheet href=/css/style.css><script>var _hmt=_hmt||[];(function(){var e,t=document.createElement("script");t.src="https://hm.baidu.com/hm.js?7c24231917964240bae97e813810620c",e=document.getElementsByTagName("script")[0],e.parentNode.insertBefore(t,e)})()</script></head><body><header>====================<br>== Hi, I'm Vimiix ==<br>====================<div style=float:right;color:gray;font-size:x-large>Get hands dirty.</div><br><p><nav><a href=https://www.vimiix.com/><b>首页</b></a>.
<a href=/posts/><b>文章列表</b></a>.
<a href=/projects/><b>开源项目</b></a>.
<a href=/tags/><b>标签</b></a>.
<a href=/friends/><b>友链</b></a>.
<a href=/about/><b>关于我</b></a>.
<a href=/index.xml><b>RSS</b></a>.</nav></p></header><main><article><h1>自助购物机交易源码</h1><b><time>2017.04.05 18:57</time></b>
<a href=/tags/source-code>source code</a>
<a href=/tags/python>Python</a><div><p><figure><img src=http://vimiix-blog.oss-cn-qingdao.aliyuncs.com/self-service-machine.jpg alt></figure></p><h5 id=本文是一篇纯源码功能上实现了自助购物机的基本交易过程>本文是一篇纯源码，功能上实现了自助购物机的基本交易过程。</h5><h6 id=由于没有去深研究实际生产环境中自助购物机的逻辑所以在逻辑上不排除有考虑不周的地方如果有不完善的地方请在评论里指出互相学习>(由于没有去深研究实际生产环境中自助购物机的逻辑，所以在逻辑上不排除有考虑不周的地方，如果有不完善的地方，请在评论里指出，互相学习。)</h6><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>	<span style=color:#75715e>#coding=utf-8</span>
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#Author:Vimiix</span>
</span></span><span style=display:flex><span>	<span style=color:#75715e>#Time:2017-04-05</span>
</span></span><span style=display:flex><span>	<span style=color:#75715e>#Lisence:Apache</span>
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#流程分析：</span>
</span></span><span style=display:flex><span>	<span style=color:#75715e>#   1、顾客投币，投币结束</span>
</span></span><span style=display:flex><span>	<span style=color:#75715e>#   2、遍历商品，展示商品信息</span>
</span></span><span style=display:flex><span>	<span style=color:#75715e>#   3、顾客购买饮料，账户结算</span>
</span></span><span style=display:flex><span>	<span style=color:#75715e>#   4、任意时刻按q退出</span>
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	print(<span style=color:#e6db74>&#39;Welcome!&#39;</span>)
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	g_money <span style=color:#f92672>=</span> <span style=color:#ae81ff>0</span>
</span></span><span style=display:flex><span>	g_drinks_list <span style=color:#f92672>=</span> [
</span></span><span style=display:flex><span>	    (<span style=color:#e6db74>&#39;Coffee&#39;</span>,<span style=color:#ae81ff>5</span>),
</span></span><span style=display:flex><span>	    (<span style=color:#e6db74>&#39;Coco&#39;</span>,<span style=color:#ae81ff>2.5</span>),
</span></span><span style=display:flex><span>	    (<span style=color:#e6db74>&#39;Milk&#39;</span>,<span style=color:#ae81ff>3</span>),
</span></span><span style=display:flex><span>	    (<span style=color:#e6db74>&#39;Water&#39;</span>,<span style=color:#ae81ff>1</span>)
</span></span><span style=display:flex><span>	    ]
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#遍历商品，打印商品列表</span>
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>def</span> <span style=color:#a6e22e>show_drink_list</span>():
</span></span><span style=display:flex><span>	    print(<span style=color:#e6db74>&#39;*********Drink List*********&#39;</span>)
</span></span><span style=display:flex><span>	    print(<span style=color:#e6db74>&#39;Index</span><span style=color:#ae81ff>\t</span><span style=color:#e6db74>Drink</span><span style=color:#ae81ff>\t</span><span style=color:#e6db74>Price&#39;</span>)
</span></span><span style=display:flex><span>	    print(<span style=color:#e6db74>&#39;----------------------------&#39;</span>)
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>for</span> index,drink <span style=color:#f92672>in</span> enumerate(g_drinks_list):
</span></span><span style=display:flex><span>	        print index,<span style=color:#e6db74>&#39;</span><span style=color:#ae81ff>\t</span><span style=color:#e6db74>&#39;</span>,drink[<span style=color:#ae81ff>0</span>],<span style=color:#e6db74>&#39;</span><span style=color:#ae81ff>\t</span><span style=color:#e6db74>&#39;</span>,drink[<span style=color:#ae81ff>1</span>]
</span></span><span style=display:flex><span>	    print(<span style=color:#e6db74>&#39;****************************&#39;</span>)
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#退款退出</span>
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>def</span> <span style=color:#a6e22e>end_shop</span>():
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>global</span> g_money
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>if</span> g_money <span style=color:#f92672>==</span> <span style=color:#ae81ff>0</span>:
</span></span><span style=display:flex><span>	        print(<span style=color:#e6db74>&#39;Your balance is 0 yuan.</span><span style=color:#ae81ff>\n</span><span style=color:#e6db74>Byebye&#39;</span>)
</span></span><span style=display:flex><span>	        exit()
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>else</span>:
</span></span><span style=display:flex><span>	        print(<span style=color:#e6db74>&#39;Return your balance:</span><span style=color:#e6db74>%d</span><span style=color:#e6db74> yuan</span><span style=color:#ae81ff>\n</span><span style=color:#e6db74>Byebye&#39;</span><span style=color:#f92672>%</span>g_money)
</span></span><span style=display:flex><span>	        exit()
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#顾客投币，投币结束以后进入选商品阶段，按q退出</span>
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>def</span> <span style=color:#a6e22e>recharge</span>():
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>global</span> g_money
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>while</span> <span style=color:#66d9ef>True</span>:
</span></span><span style=display:flex><span>	        start_money <span style=color:#f92672>=</span> raw_input(<span style=color:#e6db74>&#39;Please input your money(Press &#34;q&#34; to exit):&#39;</span>)
</span></span><span style=display:flex><span>	        i_money <span style=color:#f92672>=</span> <span style=color:#ae81ff>0</span>
</span></span><span style=display:flex><span>	        <span style=color:#66d9ef>if</span> start_money <span style=color:#f92672>==</span> <span style=color:#e6db74>&#39;q&#39;</span>:
</span></span><span style=display:flex><span>	            end_shop()
</span></span><span style=display:flex><span>	        <span style=color:#66d9ef>elif</span> start_money<span style=color:#f92672>.</span>isdigit() <span style=color:#f92672>and</span> int(start_money) <span style=color:#f92672>&gt;</span> <span style=color:#ae81ff>0</span>:
</span></span><span style=display:flex><span>	            i_money <span style=color:#f92672>=</span> int(start_money)
</span></span><span style=display:flex><span>	            g_money <span style=color:#f92672>+=</span> i_money
</span></span><span style=display:flex><span>	            print(<span style=color:#e6db74>&#39;You just input </span><span style=color:#e6db74>%d</span><span style=color:#e6db74> yuan.Your acount totaly has </span><span style=color:#e6db74>%d</span><span style=color:#e6db74> yuan&#39;</span><span style=color:#f92672>%</span>(i_money,g_money))
</span></span><span style=display:flex><span>	            <span style=color:#66d9ef>if</span> (raw_input(<span style=color:#e6db74>&#39;Do you want to continue inputing?(y/n)：&#39;</span>) <span style=color:#f92672>==</span> <span style=color:#e6db74>&#39;y&#39;</span>):
</span></span><span style=display:flex><span>	                <span style=color:#66d9ef>continue</span>
</span></span><span style=display:flex><span>	            <span style=color:#66d9ef>else</span>:
</span></span><span style=display:flex><span>	                print(<span style=color:#e6db74>&#39;Your acount totaly has </span><span style=color:#e6db74>%d</span><span style=color:#e6db74> yuan.&#39;</span><span style=color:#f92672>%</span>g_money)
</span></span><span style=display:flex><span>	                <span style=color:#66d9ef>break</span>
</span></span><span style=display:flex><span>	        <span style=color:#66d9ef>else</span>:
</span></span><span style=display:flex><span>	            print(<span style=color:#e6db74>&#34;Sorry,can&#39;t recogonize what your input.Try again.&#34;</span>)
</span></span><span style=display:flex><span>	            <span style=color:#66d9ef>continue</span>
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#75715e>#顾客挑选商品，并显示余额</span>
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>def</span> <span style=color:#a6e22e>pick_drink</span>():
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>global</span> g_money
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>global</span> g_drinks_list
</span></span><span style=display:flex><span>	    <span style=color:#66d9ef>while</span> <span style=color:#66d9ef>True</span>:
</span></span><span style=display:flex><span>	        customer_choose <span style=color:#f92672>=</span> raw_input(<span style=color:#e6db74>&#39;Input the INDEX of which you want(Press &#34;q&#34; to exit):&#39;</span>)
</span></span><span style=display:flex><span>	        <span style=color:#66d9ef>if</span> customer_choose <span style=color:#f92672>==</span> <span style=color:#e6db74>&#39;q&#39;</span>:
</span></span><span style=display:flex><span>	            end_shop()
</span></span><span style=display:flex><span>	        <span style=color:#66d9ef>else</span>:
</span></span><span style=display:flex><span>	            i_customer_choose <span style=color:#f92672>=</span> int(customer_choose)
</span></span><span style=display:flex><span>	            <span style=color:#66d9ef>if</span>( <span style=color:#ae81ff>0</span> <span style=color:#f92672>&lt;=</span> i_customer_choose  <span style=color:#f92672>and</span> i_customer_choose <span style=color:#f92672>&lt;</span> len(g_drinks_list)):
</span></span><span style=display:flex><span>	                <span style=color:#66d9ef>if</span> g_money <span style=color:#f92672>&lt;</span> g_drinks_list[i_customer_choose][<span style=color:#ae81ff>1</span>]:
</span></span><span style=display:flex><span>	                    is_recharge <span style=color:#f92672>=</span> raw_input(<span style=color:#e6db74>&#34;Your balance is not enough.Do you want to recharge?(y/n):&#34;</span>)
</span></span><span style=display:flex><span>	                    <span style=color:#66d9ef>if</span> is_recharge <span style=color:#f92672>==</span> <span style=color:#e6db74>&#39;y&#39;</span>:
</span></span><span style=display:flex><span>	                        recharge()
</span></span><span style=display:flex><span>	                    <span style=color:#66d9ef>else</span>:
</span></span><span style=display:flex><span>	                        end_shop()
</span></span><span style=display:flex><span>	                <span style=color:#66d9ef>else</span>:
</span></span><span style=display:flex><span>	                    g_money <span style=color:#f92672>-=</span> g_drinks_list[i_customer_choose][<span style=color:#ae81ff>1</span>] 
</span></span><span style=display:flex><span>	                    print(<span style=color:#e6db74>&#39;Your chooes is </span><span style=color:#e6db74>%s</span><span style=color:#e6db74> ,which costs </span><span style=color:#e6db74>%.1f</span><span style=color:#e6db74> yuan.Your balance is </span><span style=color:#e6db74>%.1f</span><span style=color:#e6db74> yuan.&#39;</span>\
</span></span><span style=display:flex><span>	                          <span style=color:#f92672>%</span>(g_drinks_list[i_customer_choose][<span style=color:#ae81ff>0</span>],g_drinks_list[i_customer_choose][<span style=color:#ae81ff>1</span>],g_money))
</span></span><span style=display:flex><span>	            <span style=color:#66d9ef>else</span>:
</span></span><span style=display:flex><span>	                print(<span style=color:#e6db74>&#34;Sorry,can&#39;t find it.&#34;</span>)
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>if</span> __name__ <span style=color:#f92672>==</span> <span style=color:#e6db74>&#39;__main__&#39;</span>:
</span></span><span style=display:flex><span>	    recharge()
</span></span><span style=display:flex><span>	    show_drink_list()
</span></span><span style=display:flex><span>	    pick_drink()
</span></span></code></pre></div></div></article></main><aside><div><div><h3>LATEST POSTS</h3></div><div><ul><li><a href=/posts/2025-10-16-kubernetes-apiserver-authorization-mechanism/>Kubernetes APIServer 鉴权机制</a></li><li><a href=/posts/2025-09-30-kubernetes-apiserver-authentication-mechanism/>Kubernetes APIServer 认证机制</a></li><li><a href=/posts/2024-12-16-deploy-kubernetes-by-kubeadm/>使用 kubeadm 搭建 kubernetes 集群</a></li><li><a href=/posts/2024-09-20-how-to-code-review/>如何做code review</a></li><li><a href=/posts/2024-08-12-weakref-in-python/>Python中的弱引用</a></li></ul></div></div></aside><footer><p>Social Links:
<a href=https://github.com/vimiix><b>Github</b></a>.
<a href=https://www.douban.com/people/vimiix/><b>Douban</b></a>.
<a href=mailto:i@vimiix.com><b>Email</b></a>.<br><hr>&copy; 2017-2025
Vimiix Yao; All rights reserved.
<a href=https://beian.miit.gov.cn/>京ICP备19015214号-1</a></p><script src=https://l2dwidget.js.org/lib/L2Dwidget.min.js></script><script>L2Dwidget.init({model:{jsonPath:"https://unpkg.com/live2d-widget-model-tororo@1.0.5/assets/tororo.model.json"}})</script></footer></body></html>